
package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.ConsumeDataVo;
import com.ruoyi.system.enums.CommissionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TConsumeRecordsMapper;
import com.ruoyi.system.domain.TConsumeRecords;
import com.ruoyi.system.service.ITConsumeRecordsService;

/**
 * 消耗记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-16
 */
@Service
public class TConsumeRecordsServiceImpl implements ITConsumeRecordsService 
{
    @Autowired
    private TConsumeRecordsMapper tConsumeRecordsMapper;

    @Override
    public ConsumeDataVo summary(Long userId) {
        //        consumeDataVo.setAllConsume(tConsumeRecordsMapper.amountByConsume(userId,null));
//        consumeDataVo.setNowConsume(tConsumeRecordsMapper.amountByConsume(userId,1));
//        consumeDataVo.setAllCamExam(tConsumeRecordsMapper.amountByCamExam(userId,null));
//        consumeDataVo.setNowCamExam(tConsumeRecordsMapper.amountByCamExam(userId,1));
//        consumeDataVo.setAllPerExam(tConsumeRecordsMapper.amountByPerExam(userId,null));
//        consumeDataVo.setNowPerExam(tConsumeRecordsMapper.amountByPerExam(userId,1));
        return new ConsumeDataVo();
    }

    /**
     * 查询消耗记录
     * 
     * @param id 消耗记录主键
     * @return 消耗记录
     */
    @Override
    public TConsumeRecords selectTConsumeRecordsById(Long id)
    {
        return tConsumeRecordsMapper.selectTConsumeRecordsById(id);
    }

    /**
     * 查询消耗记录列表
     * 
     * @param tConsumeRecords 消耗记录
     * @return 消耗记录
     */
    @Override
    public List<TConsumeRecords> selectTConsumeRecordsList(TConsumeRecords tConsumeRecords)
    {
        return tConsumeRecordsMapper.selectTConsumeRecordsList(tConsumeRecords);
    }

    /**
     * 新增消耗记录
     * 
     * @param tConsumeRecords 消耗记录
     * @return 结果
     */
    @Override
    public int insertTConsumeRecords(TConsumeRecords tConsumeRecords)
    {
        if (tConsumeRecords.getSettlement().equals(CommissionEnum.JF.getMsg())) {
            if (tConsumeRecords.getNum() != null && tConsumeRecords.getConsume() != null) {
                //单个成本计算
                BigDecimal cost = tConsumeRecords.getNum().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : tConsumeRecords.getConsume().divide(tConsumeRecords.getNum(), 2, RoundingMode.HALF_UP);
                tConsumeRecords.setCost(cost);
            }
            if (tConsumeRecords.getNum() != null && tConsumeRecords.getPrice() != null) {
                //总售价
                BigDecimal totalPrice = tConsumeRecords.getPrice().multiply(tConsumeRecords.getNum());
                //公司收益（毛利）
                BigDecimal campanyEarn = totalPrice.subtract(tConsumeRecords.getConsume());
                //公司收益
                BigDecimal camExam = campanyEarn.multiply(BigDecimal.valueOf(0.8));
                //个人收益
                BigDecimal personalEarn = campanyEarn.multiply(BigDecimal.valueOf(0.2));
                tConsumeRecords.setCampanyEarn(campanyEarn);
                tConsumeRecords.setCamEarn(camExam);
                tConsumeRecords.setPersonalEarn(personalEarn);
            }
        }else if (tConsumeRecords.getSettlement().equals(CommissionEnum.DT.getMsg())){
            if (tConsumeRecords.getPayment() != null && tConsumeRecords.getConsume() != null){
                //公司收益(毛利)
                BigDecimal campanyEarn = tConsumeRecords.getPayment().subtract(tConsumeRecords.getConsume());
                //公司收益
                BigDecimal camExam = campanyEarn.multiply(BigDecimal.valueOf(0.8));
                //个人收益
                BigDecimal personalEarn = campanyEarn.multiply(BigDecimal.valueOf(0.2));
                tConsumeRecords.setCampanyEarn(campanyEarn);
                tConsumeRecords.setCamEarn(camExam);
                tConsumeRecords.setPersonalEarn(personalEarn);
            }
        }
        tConsumeRecords.setCreateTime(DateUtils.getNowDate());
        return tConsumeRecordsMapper.insertTConsumeRecords(tConsumeRecords);
    }

    /**
     * 修改消耗记录
     * 
     * @param tConsumeRecords 消耗记录
     * @return 结果
     */
    @Override
    public int updateTConsumeRecords(TConsumeRecords tConsumeRecords)
    {
        if (tConsumeRecords.getSettlement().equals(CommissionEnum.JF.getMsg())) {
            if (tConsumeRecords.getNum() != null && tConsumeRecords.getConsume() != null) {
                //单个成本计算
                BigDecimal cost = tConsumeRecords.getNum().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : tConsumeRecords.getConsume().divide(tConsumeRecords.getNum(), 2, RoundingMode.HALF_UP);
                tConsumeRecords.setCost(cost);
            }
            if (tConsumeRecords.getNum() != null && tConsumeRecords.getPrice() != null) {
                //总售价
                BigDecimal totalPrice = tConsumeRecords.getPrice().multiply(tConsumeRecords.getNum());
                //公司收益
                BigDecimal campanyEarn = totalPrice.subtract(tConsumeRecords.getConsume());
                //公司收益
                BigDecimal camExam = campanyEarn.multiply(BigDecimal.valueOf(0.8));
                //个人收益
                BigDecimal personalEarn = campanyEarn.multiply(BigDecimal.valueOf(0.2));
                tConsumeRecords.setCampanyEarn(campanyEarn);
                tConsumeRecords.setCamEarn(camExam);
                tConsumeRecords.setPersonalEarn(personalEarn);
            }
        }else if (tConsumeRecords.getSettlement().equals(CommissionEnum.DT.getMsg())){
            if (tConsumeRecords.getPayment() != null && tConsumeRecords.getConsume() != null){
                //公司收益(毛利)
                BigDecimal campanyEarn = tConsumeRecords.getPayment().subtract(tConsumeRecords.getConsume());
                //公司收益
                BigDecimal camExam = campanyEarn.multiply(BigDecimal.valueOf(0.8));
                //个人收益
                BigDecimal personalEarn = campanyEarn.multiply(BigDecimal.valueOf(0.2));
                tConsumeRecords.setCampanyEarn(campanyEarn);
                tConsumeRecords.setCamEarn(camExam);
                tConsumeRecords.setPersonalEarn(personalEarn);
            }
        }
        return tConsumeRecordsMapper.updateTConsumeRecords(tConsumeRecords);
    }

    /**
     * 批量删除消耗记录
     * 
     * @param ids 需要删除的消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteTConsumeRecordsByIds(Long[] ids)
    {
        return tConsumeRecordsMapper.deleteTConsumeRecordsByIds(ids);
    }

    /**
     * 删除消耗记录信息
     * 
     * @param id 消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteTConsumeRecordsById(Long id)
    {
        return tConsumeRecordsMapper.deleteTConsumeRecordsById(id);
    }

    @Override
    public ConsumeDataVo allSummary(TConsumeRecords tConsumeRecords) {
        ConsumeDataVo consumeDataVo = new ConsumeDataVo();
        consumeDataVo.setAllConsume(tConsumeRecordsMapper.amountByConsume(tConsumeRecords));
        consumeDataVo.setAllCamExam(tConsumeRecordsMapper.amountByCamExam(tConsumeRecords));
        consumeDataVo.setAllPerExam(tConsumeRecordsMapper.amountByPerExam(tConsumeRecords));
        return consumeDataVo;
    }

    @Override
    public ConsumeDataVo mySummary(TConsumeRecords tConsumeRecords) {
        ConsumeDataVo consumeDataVo = new ConsumeDataVo();
        consumeDataVo.setAllConsume(tConsumeRecordsMapper.amountByConsume(tConsumeRecords));
        consumeDataVo.setAllCamExam(tConsumeRecordsMapper.amountByCamExam(tConsumeRecords));
        consumeDataVo.setAllPerExam(tConsumeRecordsMapper.amountByPerExam(tConsumeRecords));
        return consumeDataVo;
    }
}
